<style>
</style>

<section id="toolbar"></section>

<section type="template" id="template">
<a-row>
    <a-col>
        <a-title key="title1602560000337" data-name="年终考核" data-name_i18n='{"en":"年终考核"}'></a-title>
    </a-col>
</a-row>
<a-row>
    <a-col>
        <a-group-title key="group1602560000337" data-name="业务标题"></a-group-title>
    </a-col>
</a-row>
<a-row>
    <a-col>
        <a-date key="Date1603763540325" data-name="考核时间" data-required-formula="'true'" data-format="YYYY-MM"></a-date>
    </a-col>
</a-row>
<a-row>
    <a-col>
        <a-sheet key="annualAssessment1" data-name="年终考核" data-rows="1">
            <a-action-group>
            </a-action-group>
            <a-columns>
                <a-association-form key="RelevanceForm1606707043504" width="150" data-name="关联表单" data-visible="false" data-schema-code="normalInspection1" data-query-code="normalInspection" data-display-field="StaffSelector1606706834553" data-mappings="StaffSelector1606706834553:{annualAssessment1.StaffSelector1606707097379};StaffSelector1606706919063:{annualAssessment1.detachment};StaffSelector1606706921687:{annualAssessment1.detachment};ShortText1606706925435:{annualAssessment1.duty};ShortText1606706933000:{annualAssessment1.gender}"></a-association-form>
                <a-user-selector key="StaffSelector1606707097379" width="150" data-name="姓名" data-dept-visible="user" data-default-value="[]" data-org-root="[]"></a-user-selector>
                <a-text key="ShortText1607135649970" width="150" data-name="季度一" data-readonly-formula="true"></a-text>
                <a-text key="text1607135651785" width="150" data-name="季度二" data-readonly-formula="true"></a-text>
                <a-text key="text1607135652420" width="150" data-name="季度三" data-readonly-formula="true"></a-text>
                <a-text key="text1607135653113" width="150" data-name="季度四" data-readonly-formula="true"></a-text>
                <a-user-selector key="detachment" width="150" data-name="支队（处）" data-readonly-formula="true" data-dept-visible="user" data-default-value="[]" data-org-root="[]"></a-user-selector>
                <a-user-selector key="department" width="150" data-name="大队（部门）" data-readonly-formula="true" data-dept-visible="user" data-default-value="[]" data-org-root="[]"></a-user-selector>
                <a-text key="gender" width="150" data-name="性别"></a-text>
                <a-date key="birthDate" width="150" data-name="出生日期"></a-date>
                <a-text key="duty" width="150" data-name="职务"></a-text>
                <a-dropdown key="evaluationResult" width="150" data-name="考核结果" data-required-formula="'true'" data-options="优秀;称职;基本称职;不称职"></a-dropdown>
            </a-columns>
        </a-sheet>
    </a-col>
</a-row>
<a-row>
    <a-col>
        <a-attachment key="attachment" data-name="附件" data-data-item-name="attachment"></a-attachment>
    </a-col>
</a-row>
<a-row>
    <a-col>
        <a-user-selector key="entryPerson" data-name="录入人员" data-data-item-name="entryPerson" data-required-formula="'true'" data-dept-visible="user" data-default-value="[]" data-org-root="[]"></a-user-selector>
    </a-col>
    <a-col>
        <a-user-selector key="approvalPerson" data-name="审核人员" data-data-item-name="approvalPerson" data-required-formula="'true'" data-dept-visible="user" data-default-value="[]" data-org-root="[]"></a-user-selector>
    </a-col>
</a-row>
</section>


<script id="customScript">
    (function(form){
        /**
         * 用户自定义JS区域
         */
        /**
         * 事件绑定，form.on
         * @param eventType 事件类型
         * @params function 事件
         * @param  cover    true, false 是否覆盖root的生命周期 默认不覆盖 
         */
        //数据加载后，渲染之前，this为window
        var userList=[{id:'',type:'3'}]
        form.on('onLoad',function(data, dataPermission){},'cover');
        //渲染完成后
        form.on('onRendered',function(data){
            let that = this
            let goodList = []//评定为好的
            let lessgood = [] //评定为较好的
            let generalList = []//评定为一般
            let worseList = []//评定为不及格
      //将考核结果进行分类
        function classification(value){
            if(value ==='好'){
                 goodList.push(value)
            }    
             if(value ==='较好'){
                 lessgood.push(value)
            }  
            if(value ==='一般'){
                 generalList.push(value)
            }   
            if(value ==='较差'){
                 worseList.push(value)
            }

        }
        function tipAndReset(mes){
            that.$confirm({ 
                            title:'警告',
                            content:mes,
                            onOk(){ 
                                console.log('onOk')
                            },
                            onCancel(){
                               console.log('onCancel')
                            }
                        })
        }
        //考核结果提示
         const subject = this.annualAssessment1.getColumnValueChange('evaluationResult');
            if(subject){
            subject.subscribe((change)=>{
            console.log(change.value,'4556576767')
           let evaluationResult=[]
           let personaList = this.annualAssessment1.value
           goodList =[]
           lessgood = []
           generalList = []
           worseList = []
            // 1，有一半以上“好”，且无“一般”和“较差”的可以确定评为优秀
            //  2，有一半以上“一般”和“较差”的可以评为 “基本称职”和“不称职”

            personaList.forEach((item, index)=>{
                console.log(index,'indexindexindexindex')
                if(index === change.rowIndex ){
                    classification(item.ShortText1607135649970)
                    classification((item.text1607135651785))
                    classification((item.text1607135652420))
                    classification((item.text1607135653113))
                }
                })
               console.log(change,'change')
                //评定为好的比例
                 let goodPercent = goodList.length/personaList.length  
                //评定为一般的比例
                 let generpercent = generalList.length/personaList.length  
                //评定为差得比例的比例
                 let worseListpercent = worseList.length/personaList.length  
                  if(change.value[change.rowIndex] ==='优秀'){
                     if( lessgood.length > 0 || generalList.length > 0){
                       let mes='有一半以上“好”，且无“一般”和“较差”的可以确定评为优秀！'
                        this.annualAssessment1.getCell(change.rowIndex, change.index).value = '';
                         tipAndReset(mes)
                      }

                  }
                 if(change.value[change.rowIndex] ==='称职'){
                    //  有一半以上“一般”和“较差”的可以评为 “基本称职”和“不称职”
                      if( goodPercent >= 0.5 && lessgood.length <= 0 && generalList.length <= 0){
                       let mes='有一半以上“好”，且无“一般”和“较差”的可以确定评为优秀！'
                        this.annualAssessment1.getCell(change.rowIndex, change.index).value = '';
                         tipAndReset(mes)
                   }

                  }
                  if(change.value[change.rowIndex] ==='基本称职'){
                     if( goodPercent >= 0.5 && lessgood.length <= 0 && generalList.length <= 0){
                         let mes='有一半以上“好”，且无“一般”和“较差”的可以确定评为优秀！'
                        this.annualAssessment1.getCell(change.rowIndex, change.index).value = '';
                         tipAndReset(mes)
                   }else if(generalList >=0.5 || worseListpercent >=0.5 ){
                       let mes=' 有一半以上“一般”和“较差”的可以评为 “基本称职”和“不称职！'
                         tipAndReset(mes)

                   }
                  }
                  if(change.value[change.rowIndex] ==='不称职'){
                  if( goodPercent >= 0.5 && lessgood.length <= 0 && generalList.length <= 0){
                          let mes='有一半以上“好”，且无“一般”和“较差”的可以确定评为优秀！'
                     this.annualAssessment1.getCell(change.rowIndex, change.index).value = '';
                        tipAndReset(mes)
                   }else if(generalList >=0.5 || worseListpercent >=0.5){
                       let mes=' 有一半以上“一般”和“较差”的可以评为 “基本称职”和“不称职！'
                         tipAndReset(mes)

                   }

                  }
               });
            }
   
            var url = config.apiHost + '/api/runtime/query/list';
            let params={
                "filters": [
                    {
                    "propertyCode": "StaffSelector1606706834553",
                    "propertyType": 5,
                    "propertyValue": ""
                    }
                ],
                "mobile": false,
                "page": 0,
                "queryCode": "normalInspection",
                "schemaCode": "normalInspection1",
                "size": 20,
                "options": {
                    "customDisplayColumns": [
                    "StaffSelector1606706834553",
                    "StaffSelector1606706921687",
                    "ShortText1606706925435",
                    "ShortText1606706933000",
                    "StaffSelector1606706834553"
                    ],
                    "queryDisplayType": "2"
                }
                }

          const user = this.annualAssessment1.getColumnValueChange('StaffSelector1606707097379');
            if(user){
   
                user.subscribe((change)=>{
                 console.log(change.value,'用户')
                   console.log(change.rowIndex,'rowIndex')
                 let list = change.value
                    if(change.value){
                        let list = change.value
                        setTimeout(function(){
                        console.log(list[change.rowIndex],'list[change.rowIndex]')
                        userList[0].id = list[change.rowIndex][0].id
                        params.filters[0].propertyValue = JSON.stringify(userList)
                        var test=[{ShortText1607135649970:'',text1607135651785:'',text1607135652420:'',text1607135653113:'',}]
                        axios.post(url,params).then(res => {
                            let data = res.data.content
                            data.map(item=>{
                                if(item.data.ShortText1603763329262 ==='一季度'){
                                    test[0].ShortText1607135649970 = item.data.ShortText1606706950200
                                }
                                if(item.data.ShortText1603763329262 ==='二季度'){
                                    test[0].text1607135651785 = item.data.ShortText1606706950200
                                }
                                if(item.data.ShortText1603763329262 ==='三季度'){
                                    test[0].text1607135652420 = item.data.ShortText1606706950200
                                } 
                                if(item.data.ShortText1603763329262 ==='四季度'){
                                test[0].text1607135653113 = item.data.ShortText1606706950200
                                }
                            })
                            let tableData = that.annualAssessment1.value
                        tableData.map((t,index)=>{
                                test.map(v=>{
                                    if(t.StaffSelector1606707097379){
                                        if(t.StaffSelector1606707097379[0].id === list[change.rowIndex][0].id){
                                            t.ShortText1607135649970 = v.ShortText1607135649970
                                            t.text1607135651785 = v.text1607135651785
                                            t.text1607135652420 = v.text1607135652420
                                            t.text1607135653113 = v.text1607135653113

                                    }
                                    }
                   
                                })
        
                            })
                            that.annualAssessment1.value = tableData
                            
                        });
                        }, 500);
                    }

           
                });
            }


        });
        //内置校验完成后，返回false阻止提交
        form.on('onValidate',function(action,data){});
        //操作前（包含自定义按钮）执行，返回false阻止按钮操作
        form.on('onPreAction',function(action,data){});
        //自定义按钮执行
        form.on('onCustomAction',function(action,data){});
        //操作完成后执行
        form.on('onActionDone',function(action,data,httpRes){});
    })
</script>